package extend_widget

import (
	"clientDemo/ui/fyne_ui/common"
	"clientDemo/ui/fyne_ui/widget"
	"clientDemo/ui/fyne_ui/widget/interface"
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/canvas"
	"fyne.io/fyne/v2/driver/desktop"
	"image/color"
)

var _ widgetcommon.IWidgetItem = (*EmptyWidget)(nil)

func init() {
	widget.RegWidgetContain(widget.Default_WidgetContainer_Name, NewEmptyWidget)

	widget.RegWidget("WidgetNode", "空节点", NewEmptyWidget1)
}

// EmptyWidget Create a custom widget that embeds Diagonal and implements Mouseable
type EmptyWidget struct {
	//widget2.BaseWidget
	*EmptyMasterContainer
	size        fyne.Size
	name        string
	isContainer bool

	*Action
	parent widgetcommon.IWidgetItem

	// 容器本地缓存
	objects []fyne.CanvasObject
}

func NewEmptyWidgetTest(name string, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	//diagonal := NewEmptyContainer(100, 100) //默认初始化时候为(10,10)
	emptyWidget := &EmptyWidget{
		EmptyMasterContainer: &EmptyMasterContainer{
			EmptyContainer: &EmptyContainer{
				MaxHeight: 100,
				MaxWidth:  100,
			},
			WidgetContainer: &WidgetContainer{
				Container: &Container{
					name: name,
				},
				positions: make(map[fyne.CanvasObject]fyne.Position),
				objects:   make([]fyne.CanvasObject, 0),
				Border: &canvas.Rectangle{
					FillColor:   color.Transparent,
					StrokeColor: color.RGBA{211, 211, 0, 255}, //黄色
					StrokeWidth: 2,
				},
			},
		},
		Action:      &Action{},
		parent:      rootNode,
		isContainer: true,
	}
	emptyWidget.WidgetContainer.ac = emptyWidget.Action
	emptyWidget.IUserInfo = common.NewWidgetUserInfo(name, "", emptyWidget)
	emptyWidget.SetBase(emptyWidget, rootNode)
	emptyWidget.BaseWidget.ExtendBaseWidget(emptyWidget)
	emptyWidget.name = name

	return emptyWidget
}
func NewEmptyWidget1(name string, skinInfo widgetcommon.IWidgetSkin, rootNode widgetcommon.IWidgetItem) widgetcommon.IWidgetItem {
	if skinInfo == nil {
		return nil
	}
	//diagonal := NewEmptyContainer(100, 100) //默认初始化时候为(10,10)
	emptyWidget := &EmptyWidget{
		EmptyMasterContainer: &EmptyMasterContainer{
			EmptyContainer: &EmptyContainer{
				MaxHeight: 100,
				MaxWidth:  100,
			},
			WidgetContainer: &WidgetContainer{
				Container: &Container{
					name: name,
				},
				positions: make(map[fyne.CanvasObject]fyne.Position),
				objects:   make([]fyne.CanvasObject, 0),
				Border: &canvas.Rectangle{
					FillColor:   color.Transparent,
					StrokeColor: color.RGBA{211, 211, 0, 255}, //黄色
					StrokeWidth: 2,
				},
			},
		},
		Action:      &Action{},
		parent:      rootNode,
		isContainer: true,
	}
	emptyWidget.WidgetContainer.ac = emptyWidget.Action
	emptyWidget.IUserInfo = common.NewWidgetUserInfo(name, skinInfo.GetSkinName(), emptyWidget)

	emptyWidget.SetBase(emptyWidget, rootNode)
	emptyWidget.BaseWidget.ExtendBaseWidget(emptyWidget)
	emptyWidget.name = name

	return emptyWidget
}
func NewEmptyWidget(size fyne.Size) widgetcommon.IWidgetItem {
	//diagonal := NewEmptyContainer(size.Width, size.Height)
	dw := &EmptyWidget{
		EmptyMasterContainer: &EmptyMasterContainer{
			EmptyContainer: &EmptyContainer{
				MaxHeight: 100,
				MaxWidth:  100,
			},
			WidgetContainer: &WidgetContainer{
				Container: &Container{
					name: "",
				},
				positions: make(map[fyne.CanvasObject]fyne.Position),
				objects:   make([]fyne.CanvasObject, 0),
				Border: &canvas.Rectangle{
					FillColor:   color.Transparent,
					StrokeColor: color.RGBA{211, 211, 0, 255}, //黄色
					StrokeWidth: 2,
				},
			},
		},
		size:        size,
		Action:      &Action{},
		isContainer: true,
	}
	dw.WidgetContainer.ac = dw.Action
	dw.IUserInfo = common.NewWidgetUserInfo("root", "", dw)
	dw.SetBase(dw, nil)
	dw.ExtendBaseWidget(dw)
	return dw
}
func (w *EmptyWidget) addWidget(obj fyne.CanvasObject, pos fyne.Position) {
	w.objects = append(w.objects, obj)
	var all []fyne.CanvasObject
	for _, v := range w.objects {
		all = append(all, v)
	}
	w.EmptyMasterContainer.SetObjectsInterface(all)
	//w.EmptyMasterContainer.AddObjects(obj)
	w.EmptyMasterContainer.SetPosition(obj, pos)
}
func (w *EmptyWidget) AddWidgetChildObj(obj widgetcommon.IWidgetItem) {
	o, ok := obj.(fyne.CanvasObject)
	if !ok {
		return
	}
	w.objects = append(w.objects, o)
	var all []fyne.CanvasObject
	for _, v := range w.objects {
		all = append(all, v)
	}
	w.EmptyMasterContainer.SetObjectsInterface(all)
}
func (w *EmptyWidget) AddWidgetContainer(objects widgetcommon.IWidgetItem, pos fyne.Position) {
	o, ok := objects.(fyne.CanvasObject)
	if !ok {
		return
	}
	w.addWidget(o, pos)
}

func (w *EmptyWidget) AddWidgetChild(objects widgetcommon.IWidgetItem, pos fyne.Position) {
	o, ok := objects.(fyne.CanvasObject)
	if !ok {
		return
	}
	w.addWidget(o, pos)
}
func (w *EmptyWidget) MinSize() fyne.Size {
	return fyne.NewSize(w.EmptyMasterContainer.MaxWidth, w.EmptyMasterContainer.MaxHeight)
}

func (w *EmptyWidget) SetMinSize(size fyne.Size) {
	w.size = size
	w.EmptyMasterContainer.EmptyContainer.SetMinSize(size)
}
func (w *EmptyWidget) GetBaseSize() fyne.Size {
	return w.BaseWidget.Size()
}
func (w *EmptyWidget) SetName(name string) {
	w.name = name
	w.EmptyMasterContainer.SetName(name)
	w.Refresh()
}

func (w *EmptyWidget) GetName() string {
	o := w.EmptyMasterContainer.WidgetContainer.Container
	return o.GetName()
}
func (w *EmptyWidget) IsContainer() bool {
	return w.isContainer
}
func (w *EmptyWidget) SetParent(node widgetcommon.IWidgetItem) {
	w.parent = node
}
func (w *EmptyWidget) GetParent() widgetcommon.IWidgetItem {
	return w.parent
}

/*
	func (w *EmptyWidget) MouseDown(ev *desktop.MouseEvent) {
		// 打印日志，记录鼠标按下事件的位置
		log.Info("EmptyWidget:%v---down-------%v  %v", w.name, ev.AbsolutePosition, ev.Position)
		w.EmptyMasterContainer.TransMouseDown(ev)
		if w.parent == nil {
			return
		}
		if w.TouchBeganCallback != nil {
			parentPos := ev.Position
			w.TouchBeganCallback(ev, parentPos, w, true)
		} else {
			//w.Action.MouseDown(ev)
			if w.parent != nil {
				// 如果直接在容器内返回 容器在容器上层的坐标
				parentPos := w.Position()
				relativePos := fyne.NewPos(parentPos.X+ev.Position.X, parentPos.Y+ev.Position.Y)
				w.parent.(widget.IEmptyWidgetTransEvent).TransMouseDown(ev, relativePos, w)
			}
		}
	}

	func (w *EmptyWidget) MouseUp(ev *desktop.MouseEvent) {
		//log.Info("EmptyWidget:%v---up-------%v  %v", w.name, ev.AbsolutePosition, ev.Position)
		w.EmptyMasterContainer.TransMouseUp(ev)
		//if w.parent == nil {
		//	return
		//}
		if w.TouchEndedCallback != nil {
			parentPos := ev.Position
			w.TouchEndedCallback(ev, parentPos, w, true)
		} else {
			//w.Action.MouseUp(ev)
			if w.parent != nil {
				// 如果直接在容器内返回 容器在容器上层的坐标
				parentPos := w.Position()
				relativePos := fyne.NewPos(parentPos.X+ev.Position.X, parentPos.Y+ev.Position.Y)
				w.parent.(widget.IEmptyWidgetTransEvent).TransMouseUp(ev, relativePos, w)
			}
		}
	}

	func (w *EmptyWidget) MouseMoved(ev *desktop.MouseEvent) {
		//log.Info("EmptyWidget:%v---move-------%v  %v", w.name, ev.AbsolutePosition, ev.Position)
		w.EmptyMasterContainer.TransMouseMoved(ev)
		//if w.parent == nil {
		//	return
		//}
		if w.TouchMovedCallback != nil {
			parentPos := ev.Position
			w.TouchMovedCallback(ev, parentPos, w, true)
		} else {
			//w.Action.MouseUp(ev)
			if w.parent != nil {
				// 如果直接在容器内返回 容器在容器上层的坐标
				parentPos := w.Position()
				relativePos := fyne.NewPos(parentPos.X+ev.Position.X, parentPos.Y+ev.Position.Y)
				w.parent.(widget.IEmptyWidgetTransEvent).TransMouseMoved(ev, relativePos, w)
			}
		}
	}
*/
func (w *EmptyWidget) TransMouseDown(ev *desktop.MouseEvent, pos fyne.Position, obj widgetcommon.IWidgetItem) {
	// 打印日志，记录鼠标按下事件的位置
	//log.Info("EmptyWidget_TransMouseDown:%v---down-------%v  %v", w.name, ev.AbsolutePosition, ev.Position)
	if w.TouchBeganCallback != nil {
		w.TouchBeganCallback(ev, pos, obj, false)
	}
	if w.parent != nil {
		w.parent.(widgetcommon.IEmptyWidgetTransEvent).TransMouseDown(ev, pos, obj)
	}
}
func (w *EmptyWidget) TransMouseUp(ev *desktop.MouseEvent, pos fyne.Position, obj widgetcommon.IWidgetItem) {
	// 打印日志，记录鼠标按下事件的位置
	//log.Info("EmptyWidget_TransMouseUp:%v---down-------%v  %v", w.name, ev.AbsolutePosition, ev.Position)
	if w.TouchEndedCallback != nil {
		w.TouchEndedCallback(ev, pos, obj, false)
	}
	if w.parent != nil {
		w.parent.(widgetcommon.IEmptyWidgetTransEvent).TransMouseUp(ev, pos, obj)
	}
}
func (w *EmptyWidget) TransMouseMoved(ev *desktop.MouseEvent, pos fyne.Position, obj widgetcommon.IWidgetItem) {
	// 打印日志，记录鼠标按下事件的位置
	//log.Info("EmptyWidget_TransMouseMoved :%v---down-------%v  %v", w.name, ev.AbsolutePosition, ev.Position)
	if w.TouchMovedCallback != nil {
		w.TouchMovedCallback(ev, pos, obj, false)
	}
	if w.parent != nil {
		w.parent.(widgetcommon.IEmptyWidgetTransEvent).TransMouseMoved(ev, pos, obj)
	}
}

func (w *EmptyWidget) MouseIn(event *desktop.MouseEvent) {
}

// MouseOut is a hook that is called if the mouse pointer leaves the element.
func (w *EmptyWidget) MouseOut() {

}

/*
func (w *EmptyWidget) CreateRenderer() fyne.WidgetRenderer {

	//return &emptyRenderer{
	//	objects:          append([]fyne.CanvasObject{w.Border}, w.objects...),
	//	objectsInterface: &w.objectsInterface,
	//	name:             w.name,
	//}
	//o := newEmptyRenderer(w)
	//o := &emptyRenderer{
	//	emptyWidget: w,
	//	border:      w.Border,
	//}
	o := &emptyRenderer{
		emptyWidget: w,
		border:      w.Border,
		//objects:          append([]fyne.CanvasObject{w.Border}, w.objects...),
		//objectsInterface: &w.objectsInterface,
		//objects: append([]fyne.CanvasObject{w.Border}),
		name: w.name,
	}
	return o
}*/

// Objects returns the list of objects in the container
func (w *EmptyWidget) Objects() []fyne.CanvasObject {
	return w.EmptyMasterContainer.objects
}

// GetPosition returns the position of the given object
func (w *EmptyWidget) GetPosition(obj fyne.CanvasObject) fyne.Position {
	return w.EmptyMasterContainer.positions[obj]
}

func (w *EmptyWidget) Refresh() {
	w.BaseWidget.Refresh()
}
